<script lang="ts" setup>
import { computed, h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { formatDate } from '@vben/utils';

import { DescriptionChange } from '#/components/description-change';
import { DictBadge } from '#/components/dict-badge';

import DictTag from '../../../../components/dict-tag/dict-tag.vue';

interface DescriptionItemSchema {
  label: string;
  field: string;
  span?: number;
  render?: (val: any) => any;
}

const props = defineProps<{ data?: any }>();
const formData = computed(() => props.data || {});

function formatDateSafe(
  date: null | string | undefined,
  format = 'YYYY-MM-DD',
) {
  if (!date) return '';
  const d = new Date(date);
  return Number.isNaN(d.getTime()) ? '' : formatDate(d, format);
}

/** 教育经历 Schema */
const eduSchema = computed<DescriptionItemSchema[]>(() => [
  { label: '院系名称', field: 'collegeName' },
  { label: '所学专业', field: 'major' },
  {
    label: '毕结业状态',
    field: 'graduationStatus',
    render: (val) =>
      h(DictTag, { type: DICT_TYPE.PER_GRADUATION_STATUS, value: val }),
  },
  {
    label: '学历',
    field: 'educationLevel',
    render: (val) =>
      h(DictTag, { type: DICT_TYPE.PER_EDUCATION_LEVEL, value: val }),
  },

  {
    label: '学位',
    field: 'degree',
    render: (val) => h(DictTag, { type: DICT_TYPE.PER_DEGREE, value: val }),
  },
  {
    label: '是否最高学历',
    field: 'isHighest',
    render: (val) =>
      h(DictBadge, {
        value: val,
      }),
  },
  {
    label: '学习形式',
    field: 'learningForm',
    render: (val) =>
      h(DictTag, { type: DICT_TYPE.PER_LEARNING_FORM, value: val }),
  },
  {
    label: '入学方式',
    field: 'admissionMethod',
    render: (val) =>
      h(DictTag, { type: DICT_TYPE.PER_ADMISSION_METHOD, value: val }),
  },
  {
    label: '是否全日制',
    field: 'isFullTime',
    render: (val) =>
      h(DictBadge, {
        value: val,
      }),
  },
  { label: '学历证书编号', field: 'certificateNo', span: 12 },
  { label: '学位证书编号', field: 'degreeCertificateNo', span: 12 },
  { label: '学历/学位证书', field: 'attachment', span: 24 },
  { label: '备注', field: 'remarks', span: 24 },
  {
    label: '状态',
    field: 'status',
    render: (val) => h(DictTag, { type: DICT_TYPE.COMMON_STATUS, value: val }),
    span: 12,
  },
  {
    label: '更新时间',
    field: 'updateTime',
    render: (val) => formatDateSafe(val),
    span: 12,
  },
]);
</script>

<template>
  <DescriptionChange
    v-if="formData"
    :info="formData"
    :schema="eduSchema"
    :change-list="formData.changeContent?.fieldChanges || []"
    :highlight-mode="formData.highlightMode"
    :change-type="formData.changeType"
    :label-width="120"
  />
</template>
